package dtaskboard.client.project.dialog;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DatePicker;

import dtaskboard.client.Constants;
import dtaskboard.client.DialogUtil;
import dtaskboard.client.project.ProjectPage;
import dtaskboard.client.service.ProjectService;
import dtaskboard.client.service.ProjectServiceAsync;


public class CreateSprintDialog extends DialogBox {
  private final TextBox plannedHours;
  private final DatePicker startDate;
  private final DatePicker endDate;

  public CreateSprintDialog(String projectName) {
    this.plannedHours = new TextBox();
    this.startDate = new DatePicker();
    this.endDate = new DatePicker();

    Panel panel = new VerticalPanel();

    Label nameLabel = new Label("Número de horas planejadas");
    Label startLabel = new Label("Data de início");
    Label endLabel = new Label("Data de fim");

    panel.add(nameLabel);
    panel.add(this.plannedHours);
    panel.add(startLabel);
    panel.add(this.startDate);
    panel.add(endLabel);
    panel.add(this.endDate);
    panel.add(new CreateSprintSubmitButton(projectName));


    super.setAnimationEnabled(true);
    super.setAutoHideEnabled(true);
    super.setHTML("Criação de sprint");
    super.setGlassEnabled(true);
    super.setModal(true);
    super.setWidget(panel);
    super.setVisible(true);
  }

  class CreateSprintSubmitButton extends Button {
    public CreateSprintSubmitButton(final String projectName) {
      this.setText("Criar sprint");
      this.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
          ProjectServiceAsync projectService = GWT.create(ProjectService.class);
          final CreateSprintDialog csd = CreateSprintDialog.this;
          Date start = csd.startDate.getValue();
          Date end = csd.endDate.getValue();
          if (start == null || end == null) {

          }
          String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
          projectService.createSprint(projectName, csd.plannedHours.getText(),
              start, end, sessionId, new AsyncCallback<Void>() {

                @Override
                public void onSuccess(Void result) {
                  DialogUtil.closableDialog("Criação de sprint",
                      "Sprint criado com sucesso.");
                  csd.hide();
                  new ProjectPage(projectName);
                }

                @Override
                public void onFailure(Throwable caught) {
                  DialogUtil.closableDialog("Criação de sprint",
                      "Erro ao criar sprint.");
                }
              });
        }
      });
    }
  }
}
